from sqlalchemy import Column, Integer, String, Date

from sqlalchemy.orm import relationship

from database import Base


class Department(Base):
    __tablename__ = "department"

    ID = Column(Integer, primary_key=True, index=True, autoincrement=True)
    name = Column(String(50))
    location = Column(String(100))

    doctors = relationship("Doctor", back_populates="departments")

    def to_dict(self):
        return {
            "ID": self.ID,
            "name": self.name,
            "location": self.location
        }

    @classmethod
    def from_dict(cls, data: dict) -> "Department":
        valid_keys = set(cls.__table__.columns.keys())
        return cls(**{k: v for k, v in data.items() if k in valid_keys})
